<?php include("../includes/header-admin.php") ?>
<title>LazySwim</title>
</head>
<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">
                <div id="sidebar">                 
                    <?php
					// include the corresponding navigation pages
                    include("../../includes/navigation.php");
					?>     
                </div>                     

            <!-- main body -->
            
			<div class="featured-post"> 
			
				<div class="post-title"><h2> <a href="index.php">Manage Store</a> - Edit Product</h2></div>
				<div class="post-entry">            
            
				<form name="viewProductsForm" action="listProducts.php" method="post">
					<input type="submit" name="submit" value="View Products">
				</form>
				<br>
				
				<?php
					require_once("../includes/dbInfo.php");
				
					// update
					if( $_POST['submit'] ) {
						$productID = $_POST['productID'];
						$name = $_POST['name'];
						$size = $_POST['size'];
						$price = $_POST['price'];
						$category = $_POST['category'];
						// if the price is updated then the margin needs to be updated. We take the current margin and add the price to it
						// if the price decreased we will be adding a negative number so the margin changes accordingly.
						// update price AFTER we update the margin!!
						mysql_query("UPDATE products SET name = '$name', size = '$size', category = '$category', margin = margin + ('$price' - price),  price = '$price'
									 WHERE productID = '$productID' ")
						or die( mysql_error());
						
						echo "<font color=\"blue\">Product Updated! </font><br><br>";
					}
					else {
						$productID = $_GET['productID'];
						// if invalid productID just return to view products
						if( $productID == null || $productID == "" ) {
							header('location: listProducts.php');
						}
					}
					
					$sql = "SELECT * FROM products WHERE productID = '$productID'";
					$product = mysql_query($sql) or die(mysql_error()); 
					$product = mysql_fetch_assoc($product);
				?>
				
				<table cellpadding=2>
				<form name="updateProduct" action="editProduct.php" method="post">
					<input type="hidden" name="productID" value="<?php echo $product[productID]; ?>">
					<tr><td align="left">Name:  </td><td align="left"> <input type="text" name="name" size="50" maxlength="50" value="<?php echo "$product[name]"; ?>"></td></tr>
					<tr><td align="left">Price: </td><td align="left"> <input type="text" name="price" size="10" maxlength="10" value="<?php echo "$product[price]"; ?>"></td></tr>
					<tr><td align="left">Size:  </td><td align="left"> <input type="text" name="size" size="10" maxlength="10" value="<?php echo "$product[size]"; ?>"></td></tr>  
					<tr><td align="left">Category </td><td>
					<!-- hard coding this... if time will do something better -->
						<select name="category">
						<option value="Female Suits" <?php if($product[category] == "Female Suits"){ echo "selected"; } ?>>Female Suits</option>
						<option value="Male Suits" <?php if($product[category] == "Male Suits"){ echo "selected"; } ?>>Male Suits</option>
						<option value="Team Clothing" <?php if($product[category] == "Team Clothing"){ echo "selected"; } ?>>Team Clothing</option>
						<option value="Training Equipment-Other" <?php if($product[category] == "Training Equipment-Other"){ echo "selected"; } ?>>Training Equipment-Other</option>
						</select>
					</td></tr>
					<tr><td colspan=2><input type="submit" name="submit"></td></tr>
				</form>
				</table>
				<!-- end of main -->

				</div>
			</div>	
				
            <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>